package com.hw

import org.apache.spark.sql.{DataFrame, SparkSession}

object Demo8LineOnColumn {
  def main(args: Array[String]): Unit = {
    val spark: SparkSession = SparkSession.builder()
      .config("spark.sql.shuffle.partitions", 1)
      .master("local")
      .appName("lineAndColumn")
      .getOrCreate()

    val stuDF: DataFrame = spark.read
      .format("csv")
      .option("sep", ",")
      .schema("name string,item string,score int")
      .load("data/stu.txt")

    import org.apache.spark.sql.functions._
    import spark.implicits._
    val lineToColumn: DataFrame = stuDF.groupBy($"name")
      .agg(sum(when($"item" === "数学", $"score").otherwise("0")) as "math"
        , sum(when($"item" === "英语", $"score").otherwise("0")) as "english")
  }



}
